package com.bmob.lover.ui.fragment

import android.view.View
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.OnLifecycleEvent
import com.qmuiteam.qmui.util.QMUIPackageHelper
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction
import com.qmuiteam.qmui.widget.grouplist.QMUICommonListItemView
import com.theone.common.ext.startActivity
import com.bmob.lover.R
import com.bmob.lover.app.util.CacheUtil
import com.theone.mvvm.core.app.util.RxHttpManager
import com.bmob.lover.app.util.UpdateAppUtil
import com.bmob.lover.data.model.enums.BackgroundType
import com.bmob.lover.databinding.FragmentSettingBinding
import com.bmob.lover.ui.activity.LoginRegisterActivity
import com.bmob.lover.ui.fragment.base.BaseFragment
import com.bmob.lover.ui.fragment.user.PasswordModifyFragment
import com.bmob.lover.ui.fragment.user.UserBgFragment
import com.theone.mvvm.base.viewmodel.BaseViewModel
import com.theone.mvvm.core.app.util.FileManager
import com.theone.mvvm.ext.qmui.*

//  ┏┓　　　┏┓
//┏┛┻━━━┛┻┓
//┃　　　　　　　┃
//┃　　　━　　　┃
//┃　┳┛　┗┳　┃
//┃　　　　　　　┃
//┃　　　┻　　　┃
//┃　　　　　　　┃
//┗━┓　　　┏━┛
//    ┃　　　┃                  神兽保佑
//    ┃　　　┃                  永无BUG！
//    ┃　　　┗━━━┓
//    ┃　　　　　　　┣┓
//    ┃　　　　　　　┏┛
//    ┗┓┓┏━┳┓┏┛
//      ┃┫┫　┃┫┫
//      ┗┻┛　┗┻┛
/**
 * @author The one
 * @date 2021-04-26 16:03
 * @describe TODO
 * @email 625805189@qq.com
 * @remark
 */
class SettingFragment:
    BaseFragment<BaseViewModel, FragmentSettingBinding>(),View.OnClickListener {

    private lateinit var mPwdModify: QMUICommonListItemView
    private lateinit var mLoginOut: QMUICommonListItemView
    private lateinit var mCheckUpdate: QMUICommonListItemView
    private lateinit var mClearCache: QMUICommonListItemView
    private lateinit var mLauncherBg: QMUICommonListItemView
    private lateinit var mDeleteFile: QMUICommonListItemView

    override fun initView(root: View) {
        getTopBar()?.setTitleWithBackBtn(R.string.setting,this)
        getDataBinding().mGroupListView.run {
            mPwdModify = createItem("账号密码")
            mLauncherBg = createItem("启动页壁纸")
            mClearCache = createItem("清除缓存")
            mLoginOut = createItem("退出登录")
            mCheckUpdate = createItem("检查更新","Ver ${QMUIPackageHelper.getAppVersion(mActivity)}")
            mDeleteFile = createSwitchItem("删除源文件","",isCheck = CacheUtil.isUploadSuccessDeleteFile(),
                listener = {
                        _, isChecked ->
                    CacheUtil.setUploadSuccessDeleteFile(isChecked)
                })
            addToGroup(mPwdModify,title = "账户与安全",listener = this@SettingFragment)
            addToGroup(mLauncherBg,title = "个性化",listener = this@SettingFragment)
            addToGroup(mCheckUpdate,mDeleteFile,mClearCache,title = "系统",listener = this@SettingFragment)
            addToGroup(mLoginOut,title = "",listener = this@SettingFragment)
        }
    }

    private fun setCacheSize(){
        val size = FileManager.getCacheFileSize()
        mClearCache.detailText = size
    }

    override fun createObserver() {

    }

    override fun onClick(v: View?) {
        when(v){
            mPwdModify -> startFragment(PasswordModifyFragment())
            mCheckUpdate -> UpdateAppUtil(mActivity,true).checkUpdate()
            mLauncherBg -> startFragment(UserBgFragment.newInstance(BackgroundType.LAUNCHER))
            mClearCache -> clearCache()
            else ->{
                showLoginOutDialog()
            }
        }
    }

    private fun clearCache(){
        val delete = FileManager.deleteCacheFile()
        if(delete){
            showSuccessTipsDialog("清理成功")
            setCacheSize()
        }else{
            showFailTipsDialog("清理失败")
        }
    }

    private fun showLoginOutDialog(){
        context?.showMsgDialog("提示","即将退出当前账号",
            listener = QMUIDialogAction.ActionListener {
                    dialog, index ->
                dialog.dismiss()
                if(index>0){
                    mAppVm.setUserInfo(null)
                    mAppVm.setLoverBind(null)
                    RxHttpManager.clearCache()
                    startActivity(LoginRegisterActivity::class.java,true)
                }
            },
            prop = QMUIDialogAction.ACTION_PROP_NEGATIVE)
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    override fun onLazyResume() {
        setCacheSize()
        super.onLazyResume()
    }

}